﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.UI.system;
using rock.common;

namespace rock.UI.baseinfo
{
    public partial class UserControlDatabaseConfig : UserControl
    {
        string IpAddress;
        string DataBaseName;
        string Username;
        string Password;
        string ConnectionString;

        public UserControlDatabaseConfig()
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;
        }
        private void UserControlDatabaseConfig_Load(object sender, EventArgs e)
        {
            String sqlCon = ConfigUtils.GetConnectionStringsConfig("rock.Properties.Settings.RentalHousingManagerConnectionString");
            String[] data = sqlCon.Split(new char[2] { ';', '=' });

            this.IpAddress = data[1];
            this.DataBaseName = data[3];
            this.Username = data[5];
            this.Password = data[7];
            ConnectionString = "Data Source=" + this.IpAddress + ";User id=" + this.Username + ";Password=" + this.Password + "; Initial Catalog=master";


            String database_bak_path = ConfigUtils.GetAppConfig("database_bak_path");
            this.label3bakPath.Text = database_bak_path;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialogResult = MessageBox.Show("数据库还原是很严肃的操作.还原操作成功程序将自动退出本次登陆,你确定需要执行吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (!dialogResult.Equals(DialogResult.OK))
                {
                    return;
                }



                DialogResult dr = this.openFileDialog1.ShowDialog();


                if (dr == DialogResult.OK)
                {
                    String fileName = openFileDialog1.SafeFileName;
                    String filePath = openFileDialog1.FileName.Replace(fileName, "");

               
                    DataBaseControl DBC = new DataBaseControl();


                    DBC.ConnectionString = ConnectionString;
                    DBC.DataBaseName = DataBaseName;
                    DBC.DataBaseOfBackupName = fileName; //@back.bak
                    DBC.DataBaseOfBackupPath = filePath;// @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/";
                   bool success = DBC.ReplaceDataBase();
                   if (success) {
                       MessageBox.Show("还原数据库成功.系统将自动退出.请重新启动系统加载新数据.");
                       Main main = (Main)this.FindForm();
                       main.Exit();
                   }

                }
            }
            catch (Exception e3)
            {

                MessageBox.Show("还原数据库失败,具体请参见:" + e3.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

           
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "备份文件|*.bak|所有文件|*.*";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;

                String database_bak_path = ConfigUtils.GetAppConfig("database_bak_path");
                saveFileDialog.FileName = database_bak_path + "\\" + DateTime.Now.ToString(rock.UI.common.StringUtils.YYYYMMDDHHMMSS) + "数据库备份文件" + ".bak";

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    String fName = saveFileDialog.FileName;

                    int index = fName.LastIndexOf("\\") + 1;
                    String name = fName.Substring(index, fName.Length - index);
                    String path = fName.Substring(0, index);

                    DataBaseControl DBC = new DataBaseControl();
                    DBC.ConnectionString = ConnectionString;
                    DBC.DataBaseName = DataBaseName;
                    DBC.DataBaseOfBackupName = name;//@"back.bak";
                    DBC.DataBaseOfBackupPath = path; //@"D:/Program Files/Microsoft SQL Server/MSSQL/Data/";
                    bool success = DBC.BackupDataBase();
                    if (success)
                    {
                        MessageBox.Show("备份数据库成功");

                    }
                }
            }
            catch (Exception e3)
            {

                MessageBox.Show("备份数据库失败,具体请参见:" + e3.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.folderBrowserDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                String path = this.folderBrowserDialog1.SelectedPath;
                ConfigUtils.UpdateAppConfig("database_bak_path", path);
                String database_bak_path = ConfigUtils.GetAppConfig("database_bak_path");
                this.label3bakPath.Text = database_bak_path;
            }
        }

       
    }
}
